package com.mlz.mlzmall.ui.utils.util;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class ListViewUtils {
	private static List <ListAdapter> listAdapters = new ArrayList<>();
	private static ListAdapter listAdapter;
	public static void setListViewHeightBasedOnChildren(ListView lv_assess,boolean isImage) {
		// 获取ListView对应的Adapter
		listAdapter = lv_assess.getAdapter();
		if (listAdapter == null) {
			return;
		}
		int totalHeight = 0;
		for (int i = 0; i < listAdapter.getCount(); i++) {
			// listAdapter.getCount()返回数据项的数目
			View listItem = listAdapter.getView(i, null, lv_assess);
			// 计算子项View 的宽高
			listItem.measure(0, 0);
			if (i == listAdapter.getCount() - 1 && isImage) {
				totalHeight += listItem.getMeasuredHeight()+68;
				System.out.println("LIST@@@QQQ"+listItem.getMeasuredHeight());
				break;
			}
			System.out.println("LIST@@@"+listItem.getMeasuredHeight()+"@@@@"+lv_assess.getDividerHeight());
			// 统计所有子项的总高度
			totalHeight += (listItem.getMeasuredHeight() + lv_assess.getDividerHeight());
		}

		ViewGroup.LayoutParams params = lv_assess.getLayoutParams();
		params.height = totalHeight;
		// listView.getDividerHeight()获取子项间分隔符占用的高度
		// params.height最后得到整个ListView完整显示需要的高度
		lv_assess.setLayoutParams(params);
	}
	public static void setListViewHeightBasedOnChildren(ListView[] listViews) {
		for (int i = 0;i < listViews.length;i++){
			listAdapters.add(listViews[i].getAdapter());
		}
		if(listAdapters.size()==0){
			return;
		}else {

			for (int d = 0; d < listAdapters.size();d++) {
				listAdapter = listAdapters.get(d);
				if (listAdapter != null) {
					int totalHeight = 0;
					for (int i = 0; i < listAdapter.getCount(); i++) {
						// listAdapter.getCount()返回数据项的数目
						View listItem = listAdapter.getView(i, null, listViews[d]);
						// 计算子项View 的宽高
						listItem.measure(0, 0);
						// 统计所有子项的总高度
						totalHeight += (listItem.getMeasuredHeight() + listViews[d].getDividerHeight()) + 5;
					}
					ViewGroup.LayoutParams params = listViews[d].getLayoutParams();
					params.height = totalHeight + (listViews[d].getDividerHeight() * listAdapter.getCount());
					// listView.getDividerHeight()获取子项间分隔符占用的高度
					// params.height最后得到整个ListView完整显示需要的高度
					listViews[d].setLayoutParams(params);
				}
			}
		}

	}
}
